#include "optionitem.h"
#include "ui_optionitem.h"
#include <QFile>
#include <option.h>
#include <QDebug>
#include <QToolButton>
#include "optionedit.h"
OptionItem::OptionItem(QWidget *parent):
    QWidget(parent),
    ui(new Ui::OptionItem)
{
    ui->setupUi(this);

}

void OptionItem::update(Option option){
    ui->label_name->setText(option.name);
    ui->lable_image->showURLImage(option.pic);
    ui->label_amount->setText("数量: "+QString::number(option.amount));
    ui->label_type->setText(option.direction==1?"入":"出");
    ui->btn_checkBox->setIcon(QIcon(":/img/res/img/checkBox_empty.png"));

    this->option = option;
    if(option.status==1)
    {
        ui->btn_delete->setParent(0);
        ui->btn_edit->setParent(0);
        ui->horizontalLayout->removeWidget(ui->btn_delete);
        ui->horizontalLayout->removeWidget(ui->btn_edit);
    }
    else{
        connect(ui->btn_edit,&QToolButton::clicked,this,[=](){
            OptionEdit* optionEdit = new OptionEdit(this);
            optionEdit->update(option);
            optionEdit->show();
        });
        connect(ui->btn_delete,&QToolButton::clicked,this,[=](){
//            OptionEdit* optionEdit = new OptionEdit(this);
//            optionEdit->update(option);
//            optionEdit->show();
            emit deleteSignal();
        });
    }
    status = 0;
    connect(ui->btn_checkBox,&QToolButton::clicked,[=](){
        switch(status)
        {
            case 0:
                status = 2;
                ui->btn_checkBox->setIcon(QIcon(":/img/res/img/checkBox_all.png"));
                break;
            case 2:
                status = 0;
                ui->btn_checkBox->setIcon(QIcon(":/img/res/img/checkBox_empty.png"));
                break;
        }
        qDebug()<<"操作状态改变"<<status<<option.id;
        emit changeStatus(status,option.id);
    });
}
void OptionItem::setSelected(){
    status = 2;
    ui->btn_checkBox->setIcon(QIcon(":/img/res/img/checkBox_all.png"));
    qDebug()<<"setSelected操作状态改变"<<status<<option.id;
    emit changeStatus(status,option.id);
}
void OptionItem::clearSelected()
{
    status = 0;
    ui->btn_checkBox->setIcon(QIcon(":/img/res/img/checkBox_empty.png"));
    qDebug()<<"clearSelected操作状态改变"<<status<<option.id;
    emit changeStatus(status,option.id);
}
OptionItem::~OptionItem()
{
    delete ui;
}
